package com.tangding.hprose.api.server.command.impl.role;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tangding.common.exception.GException;
import com.tangding.hprose.api.server.command.Command;
import com.tangding.hprose.api.server.command.Response;
import com.tangding.service.dao.role.PermissionRepository;
import com.tangding.service.dao.role.RoleRepository;
import com.tangding.service.entity.role.Permission;
import com.tangding.service.entity.role.Role;

/**
 * @description 
 *
 * @author Ranger
 * @version 2017年12月1日_下午8:34:30
 *
 */
@Component
public class GetRole implements Command{
	
	@Autowired
	RoleRepository roleRepository;
	
	@Autowired
	PermissionRepository permissionRepository;
	
	@Override
	public Response exe(Object... params) throws GException {
		Integer roleId = Integer.parseInt(String.valueOf(params[0]));
		Role role = roleRepository.getRole(roleId);

		Set<Permission> permissions = new HashSet<>();
		List<Integer> permissionIds = role.getPermission();
		if (permissionIds != null) {
			permissionIds.forEach(permissionId -> {
				if (permissionRepository.getPermissions().containsKey(permissionId)) {
					permissions.add(permissionRepository.getPermissions().get(permissionId));
				}
			});
		}
		
		Map<String, Object> result = new HashMap<>();
		result.put("permissions", permissions);
		
		return doResponseSucc(result);
	}

}
